import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

typedef SearchHandler<T> = void Function(T? param, String searchMessage);

class SearchView<T> extends StatefulWidget {
  final Widget? prefix;
  final Widget? suffix;
  final SearchHandler<T> searchHandler;
  final String? defaultText;
  final T? param; // 参数
  final EdgeInsetsGeometry? contentPadding;

  const SearchView({required this.searchHandler, this.prefix, this.suffix, this.defaultText, this.param,
    this.contentPadding,  super.key});

  @override
  State<StatefulWidget> createState() => _SearchState();
}

class _SearchState extends State<SearchView> {
  final controller = TextEditingController();
  String _searchText = '';

  @override
  void initState() {
    super.initState();
    if (widget.defaultText != null) {
      controller.text = widget.defaultText ?? '';
    }
  }

  @override
  Widget build(BuildContext context) {
    if (widget.suffix != null) {
      return Row(children: [
        Expanded(child: _buildSearchPane()),
        Padding(padding: const EdgeInsets.only(left: 10), child: widget.suffix!,)
      ],);
    }
    return _buildSearchPane();
  }

  Widget _buildSearchPane() {
    var padding = widget.contentPadding == null ? EdgeInsets.only(top: -10.0) : widget.contentPadding;
    return Container(
      height: 40,
      padding: const EdgeInsets.only(left: 8, right: 8),
      decoration: const BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(10))),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          if (widget.prefix == null) const Icon(Icons.search, color: Colors.grey, size: 18),
          if (widget.prefix != null) widget.prefix!,
          Expanded(
              child: ConstrainedBox(
                constraints: const BoxConstraints(minWidth: 30),
                child: TextField(
                  controller: controller,
                  decoration: InputDecoration(
                      contentPadding: padding,
                      hintText: '搜索',
                      border: InputBorder.none,
                      fillColor: Colors.white,
                      hoverColor: Colors.white,
                      filled: true,
                      hintStyle: const TextStyle(color: Colors.black12),
                      suffix: _searchText == '' ? null : IconButton(
                        icon: const Icon(Icons.cancel),
                        color: Colors.grey,
                        hoverColor: Colors.white,
                        iconSize: 18.0,
                        onPressed: () {
                          controller.clear();
                          this.widget.searchHandler(this.widget.param, '');
                        },
                      )
                  ),
                  onChanged: onSearchTextChanged,
                ),
              )),
        ],
      ),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  void onSearchTextChanged(String text) {
    delayedSubmit(text);
    setState(() {
      this._searchText = text;
    });
  }

  void delayedSubmit(String searchText) {
    Future.delayed(Duration(milliseconds: 600), () {
      if (searchText == this._searchText) {
        this.widget.searchHandler(this.widget.param, searchText);
      }
    });
  }
}
